package com.imc.api.shopmall.service;

import com.imc.api.order.repository.IWxMpOrderRepository;
import com.imc.api.order.repository.IWxOrderDetailRepository;
import com.imc.api.shopmall.dao.ShopMallMapper;
import com.imc.api.shopmall.dao.ShopMallRepository;
import com.imc.api.shopmall.pojo.ShopMall;
import com.imc.platform.base.user.dao.repository.UserRepository;
import com.imc.platform.base.user.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by tanweiping on 2018/9/25.
 */
@Slf4j
@Transactional
@Service
public class ShopMallService {

    @Autowired
    ShopMallRepository shopMallRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    ShopMallMapper shopMallMapper;

    @Autowired
    IWxOrderDetailRepository iWxOrderDetailRepository;

    @Autowired
    IWxMpOrderRepository iWxMpOrderRepository;

    /*
     * tanweiping  2018/9/25 16:16
     * 方法描述：获取购物车列表
     */
    public List <Map <String, Object>> getShopMallList(Principal principal) {
        return shopMallMapper.findAllByLoginName(principal.getName());
    }

    /*
     * YT  2018/10/28 22:02
     * 方法描述：
     */
    public ShopMall postMall(Principal principal, ShopMall shopMall) throws Exception {
        Date date = new Date();
        User user = userRepository.findAllByLoginName(principal.getName());

        if (shopMall.getId() == null) {
            List <ShopMall> shopMallList = shopMallRepository.countExists(String.valueOf(user.getId()), shopMall.getSkuId());
            if (!shopMallList.isEmpty()) {
                throw new Exception("同类型课程限购一个");
//                int num  = shopMall.getNum();
//                shopMall = shopMallList.get(0);
//                shopMall.setNum(shopMall.getNum()+num);
            } else {
                shopMall.setCreateTime(date);
            }
        } else {
            ShopMall shopMall_ = shopMallRepository.findOne(shopMall.getId());
            shopMall_.setNum(shopMall.getNum());
            shopMall = shopMall_;
        }

        if (StringUtils.isEmpty(shopMall.getUserId())) {
            shopMall.setUserId(String.valueOf(user.getId()));
        }
        shopMall.setUpdateTime(date);
        ShopMall shopMall1 = shopMallRepository.save(shopMall);
        return shopMall1;
    }

    /*
     * YT  2018/10/28 22:03
     * 方法描述：根据ID删除
     */
    public void deleteByIds(List <Integer> ids) {
        if (ids.isEmpty()) return;
        shopMallRepository.deleteInBatch(ids.stream().map(s -> ShopMall.builder().id(s).build()).collect(Collectors.toList()));
    }

    /*
     * tanweiping  2018/9/29 09:33
     * 方法描述：订单下单后回删购物车，逻辑删除
     */
    public void cleanShopMallByOrder(Principal principal, Integer orderId) {
        Set <String> goodsIdList = iWxOrderDetailRepository.findGoodsIdsByOrderId(orderId);
        if (goodsIdList == null || goodsIdList.isEmpty()) return;
        shopMallRepository.cleanShopMallByOrder(principal.getName(), goodsIdList);
    }

    /*
     * YT  2018/10/28 22:03
     * 方法描述：删除所有
     */
    public void cleanAll(Principal principal) {
        shopMallRepository.cleanShopMall(principal.getName());
    }
}
